﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyBlog.IService;
using MyBlog.Model;
using MyBlog.WebApi.Utility.ApiResult;
using SqlSugar;
using System;

namespace MyBlog.WebApi.Controllers
{
    [Route("api/[controller]")]
    public class BlogNewsController : ControllerBase
    {

        private readonly IBlogNewsService _iBlogNewsService;
        public BlogNewsController(IBlogNewsService iBlogNewsService)
        {
            this._iBlogNewsService = iBlogNewsService;
        }


        [Authorize]
        [HttpGet("Query")]
        public async Task<ActionResult<ApiResult>> GetBlogNews()
        {
            //  int id = Convert.ToInt32(this.User.FindFirst("Id").Value);
            // 查询所有文章
            var data = await _iBlogNewsService.QueryAsync();
            if (data == null) return ApiResultHelper.Error("没有更多的文章");
            return ApiResultHelper.Success(data);

        }

        //[HttpPost("QueryPage")]
        //public async Task<ActionResult<ApiResult>> QueryPage(int pageIndex, int pageSize)
        //{
        //    RefAsync<int> total = 0;
        //    var blogNews = await _iBlogNewsService.QueryAsync(pageIndex, pageSize, total);
        //    try
        //    {
        //        var blognewsDTO = iMapper.Map<List<BlogNewsDTO>>(blognews);
        //        return ApiResultHelper.Success(blognewsDTO, total);
        //    }
        //    catch (Exception)
        //    {
        //        return ApiResultHelper.Error("AutoMapper映射错误");
        //    }
        //}

        /// <summary>
        /// 创建文章
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="typeid"></param>
        /// <returns></returns>
        [HttpPost("Create")]
        public async Task<ActionResult<ApiResult>> Create(string title, string content, int typeid)
        {
            //数据验证
            BlogNews blogNews = new BlogNews
            {
                BrowseCount = 0,
                Content = content,
                LikeCount = 0,
                Time = DateTime.Now,
                Title = title,
                TypeId = 1, // 文章类型
                // WriterId = Convert.ToInt32(this.User.FindFirst("Id").Value) // 文章作者
                WriterId = 5155
            };
            bool b = await _iBlogNewsService.CreateAsync(blogNews);
            if (!b) return ApiResultHelper.Error("添加失败，服务器发生错误");
            return ApiResultHelper.Success(blogNews);

        }

        [HttpDelete("Delete")]
        public async Task<ActionResult<ApiResult>> Delete(int id)
        {
            // 先判断文章是否存在
            var blogNews = await _iBlogNewsService.FindAsync(id);
            if (blogNews == null) return ApiResultHelper.Error("文章不存在");
            bool b = await _iBlogNewsService.DeleteAsync(id);
            // if (!b) return ApiResultHelper.Error("删除失败，服务器发生错误");
            return ApiResultHelper.Success(b);
        }

        [HttpPut("Update")]
        public async Task<ActionResult<ApiResult>> Update(int id, string title, string content, int typeid)
        {
            // 先判断文章是否存在
            var blogNews = await _iBlogNewsService.FindAsync(id);
            if (blogNews == null) return ApiResultHelper.Error("文章不存在");

            BlogNews obj = new BlogNews
            {
                Id = id,
                BrowseCount = 0,
                Content = content,
                LikeCount = 0,
                Time = DateTime.Now,
                Title = title,
                TypeId = typeid, // 文章类型
                // WriterId = Convert.ToInt32(this.User.FindFirst("Id").Value) // 文章作者
                WriterId = 5155
            };
            var res = await _iBlogNewsService.EditAsync(obj);
            
            return ApiResultHelper.Success("更新成功");
        }
    }
}
